//
//  StatusPicturesView.swift
//  微博Swift
//
//  Created by  justinchou on 16/3/1.
//  Copyright © 2016年  justinchou. All rights reserved.
//

import UIKit

// 微博配图视图
class StatusPicturesView: UIView {
    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 公共属性
    var pic_urls: [StatusPictures]? {
        didSet{
            
            for i in 0..<9 {
                subviews[i].hidden = true
            }
            
            var index = 0
            for e in pic_urls ?? [] {
                let iv = subviews[index++] as! UIImageView
                
                iv.justin_setImageWithURL(e.thumbnail_pic, placeholderName: nil)
                
                iv.hidden = false
                
                //处理四张图片
                
                if index == 2 && pic_urls?.count == 4 {
                    index++
                }
            }
            
        }
    }
    
    // MARK: - 私有控件
    
}

// MARK: - 设置界面布局
extension StatusPicturesView {
    
    private func setupUI() {
        
        self.backgroundColor = superview?.backgroundColor
        //裁切图片
        clipsToBounds = true
        
        let layout = StatusCellViewLayout()
        
        let rect = CGRect(origin: CGPointZero, size: CGSize(width: layout.pictureWH, height: layout.pictureWH))
        let width = layout.pictureWH + layout.pictureMargin
        
        //循环添加图像视图
        for i in 0..<9 {
            let iv = UIImageView()
            
            iv.backgroundColor = UIColor.yellowColor()
            //不会拉伸图片，只会裁剪多余部分的模式
            iv.contentMode = .ScaleAspectFill
            iv.clipsToBounds = true
            
            let row = CGFloat(i / Int(layout.imagesPerRow))
            let col = CGFloat(i % Int(layout.imagesPerRow))
            
            iv.frame = CGRectOffset(rect, col * width, row * width)
            
            addSubview(iv)
        }
        
    }
}
